﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Starksoft.Cryptography.OpenPGP;

using AKMII.DMRA.DataAccess;
using AKMII.DMRA.Business.Rule;

namespace AKMII.DMRA.DataLoader
{
    public class Encryptor
    {
        private bool Encrypt(string file, string destPath, string fileName, string type)
        {
            FileStream sourceFile = null;
            FileStream outputFile = null;

            try
            {
                if (!string.IsNullOrEmpty(file))
                {
                    GnuPG gnu = new GnuPG();

                    if (type.Equals("PrintingHouse", StringComparison.CurrentCultureIgnoreCase))
                    {
                        gnu.Recipient = SystemConfiguration.CommonSetting.PrintingHouseRecipient;
                        sourceFile = new FileStream(file, FileMode.Open);
                        outputFile = new FileStream(destPath + SystemConfiguration.CommonSetting.PrintingHouseFileName,
                            FileMode.Create);
                    }
                    else if (type.Equals("DMRA", StringComparison.CurrentCultureIgnoreCase))
                    {
                        gnu.Recipient = SystemConfiguration.CommonSetting.DMRARecipient;
                        sourceFile = new FileStream(file, FileMode.Open);
                        outputFile = new FileStream(Path.Combine(destPath, fileName + ".pgp"),
                            FileMode.Create);
                    }
                    else
                    {
                        // Noting to do.
                    }

                    gnu.Encrypt(sourceFile, outputFile);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in encrypt process", ex);
                return false;
            }
            finally
            {
                if (sourceFile != null)
                {
                    sourceFile.Close();
                }
                if (outputFile != null)
                {
                    outputFile.Close();
                }
            }

            return true;
        }

        public void Process()
        {
            CommandHistoryAccessor accessor = new CommandHistoryAccessor();
            DateTime dateTime = accessor.GetLastExecuteTime(Command.Encrypt.ToString());
            FileInfo[] fi = null;
            DirectoryInfo di = null;

            foreach (var file in Directory.GetFiles(SystemConfiguration.CommonSetting.FolderForEncryptPrintingHouseFile))
            {
                File.Delete(file);
                Logger.Info("Delete history encrypted file for printing house at " + file);
            }

            foreach (var file in Directory.GetFiles(SystemConfiguration.CommonSetting.FolderForEncryptDmraFile))
            {
                File.Delete(file);
                Logger.Info("Delete history encrypted file for DMRA at " + file);
            }

            #region Encrypt file for DMRA

            di = new DirectoryInfo(SystemConfiguration.CommonSetting.FolderForGenerateDmraFile);
            fi = di.GetFiles("*.txt");

            foreach (var file in fi)
            {
                if (file.LastWriteTime > dateTime)
                {
                    Logger.Info("Start encrypt " + file.Name);

                    Encrypt(Path.Combine(SystemConfiguration.CommonSetting.FolderForGenerateDmraFile, file.Name),
                        SystemConfiguration.CommonSetting.FolderForEncryptDmraFile, file.Name, "DMRA");

                    Logger.Info("Finish encrypt " + file.Name);
                }
            }

            #endregion

            #region Encrypt file for Printing House

            di = new DirectoryInfo(SystemConfiguration.CommonSetting.FolderForGeneratePrintingHouseFile);
            fi = di.GetFiles("*.txt");

            foreach (var file in fi)
            {
                if (file.LastWriteTime > dateTime)
                {
                    Logger.Info("Start encrypt " + file.Name);

                    Encrypt(Path.Combine(SystemConfiguration.CommonSetting.FolderForGeneratePrintingHouseFile, file.Name),
                        SystemConfiguration.CommonSetting.FolderForEncryptPrintingHouseFile, file.Name, "PrintingHouse");

                    Logger.Info("Finish encrypt " + file.Name);
                }
            }

            #endregion

            accessor.UpdateLastExecuteTime(Command.Encrypt.ToString(), DateTime.Now);
        }
    }
}
